package com.bwie.shop.myrun;

import com.alibaba.fastjson.JSON;
import com.bwie.common.utils.R;
import com.bwie.shop.dao.ShopDao;
import com.bwie.shop.entity.ShopEntity;
import com.bwie.shop.feign.Feignservice;
import com.bwie.shop.vo.ComeEntityVo;
import com.bwie.shop.vo.TypeEntityVo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class MyRunner implements ApplicationRunner {
    @Autowired
    RestHighLevelClient client;
   @Autowired
    ShopDao shopDao;


    //品搜索项目启动使用ApplicationRunner进行数据同步，
    // 调用数据维护服务商品同步到ES中（3分）
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("搜索项目启动使用ApplicationRunner进行数据同步");
        List<ShopEntity> list2 = shopDao.selectList(null);
        if (list2!=null){
            BulkRequest request = new BulkRequest();
            for (ShopEntity shopEntity : list2) {
                IndexRequest request2 = new IndexRequest("lastmonth");
                request2.id(shopEntity.getSid()+"");
                String jsonString = JSON.toJSONString(shopEntity);
                request2.source(jsonString, XContentType.JSON);
                request.add(request2);
            }
            System.out.println(list2);
            BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);

        }


    }
}
